package tinybase.sm;

import tinybase.basic.RC;
import tinybase.basic.RedBase;

public class SM_Errors {

	//
	// Error table
	//
	static String[] SM_WarnMsg = { 
		"invalid DB name",
		"cannot change directory", 
		"invalid relation name",
		"duplicated attribute names", 
		"relation already exists",
		"relation not found", 
		"relation/attribute not found",
		"index already exists", 
		"index not found", 
		"data file I/O failed",
		"invalid data file format", 
		"parameter undefined" 
	};
	
	static String[] SM_ErrorMsg= {
		"no memory"
	};
	
	// 
	// SM_PrintError
	// 
	// Desc: Send a message corresponding to a SM return code to cerr
	// In:   rc - return code for which a message is desired
	//
	public static void printError(RC rc) {
		// Check the return code is within proper limits
		if ((rc.getValue() >= RedBase.START_SM_WARN) &&
				(rc.getValue() <= RC.SM_LASTWARN.getValue()))
			// Print warning
			System.err.println("RM warning: " + 
					SM_WarnMsg[rc.getValue() - RedBase.START_SM_WARN]);
		// Error codes are negative, so invert everything
		else if ((0-rc.getValue()) >= (0-RedBase.START_SM_ERR) &&
				(0-rc.getValue()) <= (0-RC.SM_LASTERROR.getValue()))
			// Print Errors
			System.err.println("RM error: " + SM_ErrorMsg[0-rc.getValue()
			        + RedBase.START_SM_ERR]);
		else if (rc.getValue() == 0)
			System.err.println("SM_PrintError called with return code of 0");
		else
			System.err.println("RM error: " + rc.getValue() + 
					" is out of bounds");
	}
}
